﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace com.calitha.goldparser.sql
{
    public class CodeTemplateWrapper
    {
        public CodeTemplateWrapper() { }
        public CodeTemplateWrapper(string template)
        {
            Load(template);
        }
        public string CodeStart { get; set; }
        public string SqlParameterStart { get; set; }
        public string SqlParameter { get; set; }
        public string SqlParameterEnd { get; set; }
        public string CodeEnd { get; set; }
        public string TemplateName { get; set; }
        public void Load(string template)
        {
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(template);
            CodeStart = doc.GetElementsByTagName("CodeStart")[0].InnerText;
            SqlParameterStart = doc.GetElementsByTagName("SqlParameterStart")[0].InnerText;
            SqlParameter = doc.GetElementsByTagName("SqlParameter")[0].InnerText;
            SqlParameterEnd = doc.GetElementsByTagName("SqlParameterEnd")[0].InnerText;
            CodeEnd = doc.GetElementsByTagName("CodeEnd")[0].InnerText;
            TemplateName=doc.GetElementsByTagName("codeTemplate")[0].Attributes.GetNamedItem("name").Value;
        }
    }
    public class CodeTemplateListWrapper : List<CodeTemplateWrapper>
    {
        public void LoadXML(string templatePatch)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(templatePatch);
            foreach (XmlNode template in doc.GetElementsByTagName("codeTemplate"))
            {
                this.Add(new CodeTemplateWrapper(template.OuterXml));   
            }
        }
        public CodeTemplateListWrapper(string templatePatch)
        {
            LoadXML(templatePatch);
        }
        public CodeTemplateWrapper SearchByName(string name)
        {
            foreach (CodeTemplateWrapper template in this)
            {
                if (template.TemplateName == name) return template;
            }
            throw new InvalidOperationException("Template not found");
        }
    }
}
